package com.krtech.wecard.utils;

import cn.hutool.core.codec.Base64;
import org.springframework.stereotype.Component;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.AlgorithmParameterSpec;

@Component
public class CommonDecUtils {

    public String wxDecrypt(String encryptedData, String sessionKey, String vi)  {
       try {
           // 开始解密
           byte[] encData = cn.hutool.core.codec.Base64.decode(encryptedData);
           byte[] iv = cn.hutool.core.codec.Base64.decode(vi);
           byte[] key = Base64.decode(sessionKey);
           AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
           Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
           SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
           cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
           return new String(cipher.doFinal(encData), "UTF-8");
       }catch (Exception e){
           e.printStackTrace();
       }
       return "";
    }






}
